package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.GwOperationAccount;
import com.ruoyi.system.domain.GwOperationAccountRole;
import com.ruoyi.system.domain.GwOperationRole;
import com.ruoyi.system.domain.GwPlatformAccount;
import com.ruoyi.system.mapper.GwOperationAccountMapper;
import com.ruoyi.system.mapper.GwOperationAccountRoleMapper;
import com.ruoyi.system.mapper.GwOperationRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.BaseService;
import com.ruoyi.system.service.IGwOperationAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 运营账号Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class GwOperationAccountServiceImpl extends BaseService implements IGwOperationAccountService
{
    @Resource
    private GwOperationAccountMapper gwOperationAccountMapper;
    @Resource
    private GwOperationAccountRoleMapper accountRoleMapper;
    @Resource
    private GwOperationRoleMapper roleMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    private static final String AVATAR = "https://gowork365.com/prod-api/profile/mini/index/enterpriseLogo.png";

    /**
     * 查询运营账号
     * 
     * @param id 运营账号主键
     * @return 运营账号
     */
    @Override
    public GwOperationAccount selectGwOperationAccountById(String id)
    {
        return gwOperationAccountMapper.selectGwOperationAccountById(id);
    }

    /**
     * 查询运营账号列表
     * 
     * @param gwOperationAccount 运营账号
     * @return 运营账号
     */
    @Override
    public List<GwOperationAccount> selectGwOperationAccountList(GwOperationAccount gwOperationAccount)
    {
        List<GwOperationAccount> list = gwOperationAccountMapper.selectGwOperationAccountList(gwOperationAccount);
        list.forEach(k->{
           String roleNames =  roleMapper.selectRoleNamesByAccount(k.getId());
           k.setRoleNames(roleNames);
        });
        return list;
    }

    /**
     * 新增运营账号
     * 
     * @param gwOperationAccount 运营账号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertGwOperationAccount(GwOperationAccount gwOperationAccount)
    {
        if(!checkPhone(gwOperationAccount.getPhone())){
            return AjaxResult.error("该手机号已注册过，不可重复注册");
        }

        gwOperationAccount.setId(IdUtils.fastSimpleUUID());
        gwOperationAccount.setCreateBy(SecurityUtils.getUserId()+"");
        gwOperationAccount.setCreateTime(DateUtils.getNowDate());
        int num = gwOperationAccountMapper.insertGwOperationAccount(gwOperationAccount);
        //统一登录账号管理，将账号插入sys_user一份
        SysUser sysUser = new SysUser();
        sysUser.setUserType(UserType.OPERATION_USER.getCode());
        sysUser.setPhonenumber(gwOperationAccount.getPhone());
        sysUser.setNickName(gwOperationAccount.getUserName());
        sysUser.setPassword(SecurityUtils.encryptPassword(gwOperationAccount.getPassword()));
        sysUser.setChannelId(gwOperationAccount.getId());
        sysUser.setAvatar(AVATAR);
        sysUserMapper.insertUser(sysUser);
        //处理账号与运营角色
        if(!CollectionUtils.isEmpty(gwOperationAccount.getRoleList())){
            for(String roleId:gwOperationAccount.getRoleList()){
                GwOperationAccountRole accountRole = new GwOperationAccountRole();
                accountRole.setId(IdUtils.fastSimpleUUID());
                accountRole.setAccountId(gwOperationAccount.getId());
                accountRole.setOperationRoleId(roleId);
                accountRoleMapper.insertGwOperationAccountRole(accountRole);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改运营账号
     * 
     * @param gwOperationAccount 运营账号
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateGwOperationAccount(GwOperationAccount gwOperationAccount)
    {
        gwOperationAccount.setUpdateTime(DateUtils.getNowDate());
        gwOperationAccount.setUpdateBy(SecurityUtils.getUserId()+"");
        int num = gwOperationAccountMapper.updateGwOperationAccount(gwOperationAccount);
        accountRoleMapper.deleteGwOperationAccountRoleByAccountId(gwOperationAccount.getId());
        //同步更新sys_user信息
        SysUser sysUser = sysUserMapper.selectUserByChannelId(gwOperationAccount.getId());
        if(sysUser!=null){
            sysUser.setPhonenumber(gwOperationAccount.getPhone());
            sysUser.setNickName(gwOperationAccount.getUserName());
            sysUser.setPassword(SecurityUtils.encryptPassword(gwOperationAccount.getPassword()));
            sysUser.setAvatar(AVATAR);
            sysUserMapper.updateUser(sysUser);
        }
        //更新运维角色
        if(!CollectionUtils.isEmpty(gwOperationAccount.getRoleList())){
            for(String roleId:gwOperationAccount.getRoleList()){
                GwOperationAccountRole accountRole = new GwOperationAccountRole();
                accountRole.setId(IdUtils.fastSimpleUUID());
                accountRole.setAccountId(gwOperationAccount.getId());
                accountRole.setOperationRoleId(roleId);
                accountRoleMapper.insertGwOperationAccountRole(accountRole);
            }
        }
        return num;
    }

    /**
     * 批量删除运营账号
     * 
     * @param ids 需要删除的运营账号主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteGwOperationAccountByIds(String[] ids)
    {
        int num = gwOperationAccountMapper.deleteGwOperationAccountByIds(ids);
        for(String id:ids){
            SysUser sysUser  = sysUserMapper.selectUserByChannelId(id);
            if(sysUser!=null){
                sysUserMapper.deleteUserById(sysUser.getUserId());
            }
        }

        return num;
    }

    /**
     * 删除运营账号信息
     * 
     * @param id 运营账号主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteGwOperationAccountById(String id)
    {
        int num = gwOperationAccountMapper.deleteGwOperationAccountById(id);
        SysUser sysUser  = sysUserMapper.selectUserByChannelId(id);
        if(sysUser!=null){
            sysUserMapper.deleteUserById(sysUser.getUserId());
        }
        return num;
    }

    @Override
    public int resetPassword(GwOperationAccount gwOperationAccount) {
        GwOperationAccount account = new GwOperationAccount();
        account.setId(gwOperationAccount.getId());
        account.setPassword(SecurityUtils.encryptPassword(gwOperationAccount.getPassword()));
        int num = gwOperationAccountMapper.updateGwOperationAccount(account);

        //同步更新sys_user用户表
        SysUser sysUser  = sysUserMapper.selectUserByChannelId(gwOperationAccount.getId());
        if(sysUser!=null){
            sysUser.setPassword(SecurityUtils.encryptPassword(gwOperationAccount.getPassword()));
            sysUserMapper.updateUser(sysUser);
        }
        return num;
    }
}
